package com.atguigu.gmall.all.controller;


import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索管理
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    //搜索
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Map map){
        SearchResponseVo vo = listFeignClient.list(searchParam);
        map.put("goodsList",vo.getGoodsList());
        map.put("pageNo",vo.getPageNo());
        map.put("totalPages",vo.getTotalPages());
        map.put("trademarkList",vo.getTrademarkList());
        map.put("attrsList",vo.getAttrsList());
        map.put("searchParam",searchParam);
        //urlParam
        String urlParam = buildUrlParam(searchParam);
        map.put("urlParam",urlParam);
        //回显选中条件品牌
        String trademarkParam = buildTrademarkParam(searchParam);
        map.put("trademarkParam",trademarkParam);
        //回显选中的属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        map.put("propsParamList",propsParamList);
        //排序
        Map orderMap = buildOrderMap(searchParam);
        map.put("orderMap",orderMap);



        return "list/index";
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        String order = searchParam.getOrder();
        if(!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);
        }else{
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        return orderMap;
    }

    //回显选中的属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {

        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            List<Map> listMap = new ArrayList<>();
            //Map : map.attrId  map.attrName   map.attrValue
            for (String prop : props) {/// 平台ID:值名称:名称
                String[] p = org.apache.commons.lang3.StringUtils.
                        split(prop, ":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                listMap.add(map);
            }
            return listMap;
        }
        return null;
    }

    //回显选中条件品牌
    private String buildTrademarkParam(SearchParam searchParam) {
        //品牌:三星      //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }


    @Value("${url.list}")
    private String urlList;
    //url + param 拼接
    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(urlList).append("?");
        //一二三级分类ID   二三级省了  不写了
        Long category1Id = searchParam.getCategory1Id();
        if(null != category1Id){
            stringBuilder.append("&category1Id=").append(category1Id);
        }
        //品牌   品牌ID:品牌名称
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            stringBuilder.append("&trademark=").append(trademark);
        }
        String keyword = searchParam.getKeyword();
        if(!StringUtils.isEmpty(keyword)){
            stringBuilder.append("&keyword=").append(keyword);
        }
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            for (String prop : props) {// 平台ID:值名称:名称
                stringBuilder.append("&props=").append(prop);
            }
        }
        return stringBuilder.toString().replaceFirst("&","");

    }
}
